// pages/order/order.js
const app = getApp()
const api = require('../../utils/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currIndex: 1,//当前页数
    isLoging: false,
    isScrollDown: false,//是否到底
    isRefresher: false//是否开启下拉刷新
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navHeight: app.globalData.navHeight,
      scrollHeight: app.globalData.scrollHeight,
      scale: app.globalData.scale
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.currIndex = 1;
    this.getorderList();
  },


  //下拉刷新
  refresher() {
    this.setData({
      isScrollDown: false,
      currIndex: 1
    })
    this.getorderList();
  },

  //滑动到底部事件
  srcollDown() {
    if (this.data.isScrollDown) return;
    this.data.currIndex++;
    this.getorderList();
    this.setData({ isLoging: true })
  },

  //获取订单列表
  getorderList() {
    console.log({
      "userId": wx.getStorageSync('userId'),
      "currIndex": this.data.currIndex,
      "pageSize": 5
    });
    api.getMyOrder({
      "userId": wx.getStorageSync('userId'),
      "currIndex": this.data.currIndex,
      "pageSize": 5
    }).then((res) => {
      console.log(res);
      if (res.data.code == '1') return;
      //console.log(res);
      let time = setTimeout(() => {
        if (res.data.data.length == '0') {
          this.setData({
            isScrollDown: true,
            isLoging: false
          })
        } else {
          let list = res.data.data;
          let time = this.getData();
          //console.log(list);
          list.forEach((i, index) => {
            let tPrice = 0;
            let sPrice = 0;
            if (!list[index].discount) {
              list[index].discount = 0;
            }
            if (i.state == 0 && time > i.payTime) {
              list[index].payUrl = ''; 
              list[index].stateNote = '订单已关闭';
              list[index].close = 1;
              list[index].goodsList.forEach(i => {
                i.stateNote = '订单已关闭';
                tPrice += i.goodsPrice * 1 * i.goodsCount;
                if (i.state != 3) {
                  sPrice += i.goodsPrice * 1 * i.goodsCount;
                }
              });
              list[index].tPrice = tPrice;
              list[index].sPrice = tPrice * 1 - list[index].discount * 1;
            } else {
              list[index].goodsList.forEach(i => {
                tPrice += i.goodsPrice * 1 * i.goodsCount;
                if (i.state != 3) {
                  sPrice += i.goodsPrice * 1 * i.goodsCount;
                }
              });
              list[index].tPrice = tPrice;
              list[index].sPrice = sPrice * 1 - list[index].discount * 1;
            }
            if (list[index].sPrice * 1 < 0) {
              list[index].discount = 0;
              list[index].sPrice = 0;
            }
          })
          this.setData({
            orderList: this.data.currIndex == '1' ? list : this.data.orderList.concat(list),
            isLoging: false,
            isRefresher: false
          })
        }
        clearTimeout(time);
      }, 300)
    })
  },

  //点击查看商品详情
  onClickGoods(e) {
    app.toOutLink(e);
  },
  //点击打开app链接
  onClickTit(e) {
    app.toOutLink(e);
  },

  //获取日期
  getData() {
    let d = new Date();
    let data = `${d.getFullYear()}-${(d.getMonth() + 1) < 10 ? ('0' + (d.getMonth() + 1)) : (d.getMonth() + 1)}-${d.getDate() < 10 ? ('0' + d.getDate()) : d.getDate()}`;
    let time = `${d.getHours() < 10 ? ('0' + d.getHours()) : d.getHours()}:${d.getMinutes() < 10 ? ('0' + d.getMinutes()) : d.getMinutes()}:${d.getSeconds() < 10 ? ('0' + d.getSeconds()) : d.getSeconds()}`;
    return data + ' ' + time;
  }

})